package com.zlx.mall.basic.web.aspect;


import com.zlx.mall.basic.web.holder.RequestContextHolder;
import com.zlx.mall.basic.web.model.BaseRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author qingke
 **/
@Aspect
@Component
@Order(-9999)
public class RequestWrapperAspect {

    @Pointcut(value = "(@annotation(org.springframework.web.bind.annotation.RequestMapping)" +
            "||@annotation(org.springframework.web.bind.annotation.PostMapping)) " +
            "||@annotation(org.springframework.web.bind.annotation.GetMapping)")
    private void pointcut() {}

    /**
     * Filter.in
     * Interceptor.preHandle
     * Aspect.Around
     * Aspect.Before
     * Method
     * Aspect.Around
     * Aspect.After
     * Aspect.AfterReturning
     * Interceptor.afterCompletion
     * Filter.out
     */
    @Before("pointcut()")
    public void doBefore(JoinPoint point) {
        for (Object arg : point.getArgs()) {
            if (arg instanceof BaseRequest) {
                ((BaseRequest) arg).build(
                        RequestContextHolder.getHeaders(),
                        RequestContextHolder.getUri(),
                        RequestContextHolder.getUriVars(),
                        RequestContextHolder.getRemoteAddress(),
                        RequestContextHolder.getCookies(),
                        RequestContextHolder.getContent());
                break;
            }
        }
    }

}
